package com.donleo.ssm.service.impl;

import com.donleo.ssm.dao.IUserMapper;
import com.donleo.ssm.dto.UserDto;
import com.donleo.ssm.model.UserModel;
import com.donleo.ssm.service.IUserService;
import com.donleo.ssm.utils.CommonPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liangd
 * date 2020-11-17 16:43
 * code
 */
@Service("userService")
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserMapper userMapper;

    @Override
    public List<UserModel> findAll(UserDto userDto) {
        return userMapper.findAll(userDto);
    }

    @Override
    public UserModel findById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public UserModel add(UserModel userModel) {
        try {
            userModel.setCreateTime(new Date());
            userMapper.add(userModel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userModel;
    }

    @Override
    public Integer delete(Integer id) {
        return userMapper.delete(id);
    }

    @Override
    public Integer deletes(List<Integer> ids) {
        return userMapper.deletes(ids);
    }

    @Override
    public Integer update(UserModel userModel) {

        userModel.setUpdateTime(new Date());
        return userMapper.update(userModel);
    }

    @Override
    public Integer getCount(UserDto userDto) {
        return userMapper.getCount(userDto);
    }

    @Override
    public Integer register(UserModel userModel) {

        boolean isExist = isExistFindByName(userModel.getUsername());
        /*
         * isExist==null,说明数据库中没有该用户，可以插入
         * 如果不为空，用户已存在，返回500状态码
         */
        if (isExist) {
            userModel.setCreateTime(new Date());
            userMapper.add(userModel);
            return 200;
        } else {
            return 500;
        }
    }

    @Override
    public Integer login(UserModel userModel) {
        Jedis jedis = new Jedis();
        jedis.auth(CommonPath.REDIS_AUTH.getValue());
        String backCode = jedis.get("backCode");
        /*
            1、200 登录成功
            2、400 验证码不正确
            3、500 账号或密码错误
         */
        if (userModel.getCode().equals(backCode)) {
            boolean isExist = isExistFindByNameAndPwd(userModel);
            //返回值为true，用户不存在,没有查询到用户，返回500错误码
            if (!isExist) {
                return 200;
            }
            return 500;
        }
        return 400;
    }

    @Override
    public Integer findUser(UserModel userModel) {
        UserModel user = userMapper.isExistFindByNameAndPwd(userModel);
        Jedis jedis = new Jedis();
        jedis.auth(CommonPath.REDIS_AUTH.getValue());
        jedis.set("userId", String.valueOf(user.getId()));
        //过期时间半个小时
//        jedis.expire("userId",1800);
        return user.getId();
    }

    /**
     * 判断用户是否存在
     *
     * @param username 用户名
     * @return Boolean
     */
    private boolean isExistFindByName(String username) {

        UserModel userModel = userMapper.findUserByName(username);
        //如果userModel==null,返回true,不为空返回false
        return userModel == null;
    }

    /**
     * 根据用户名和密码查询
     *
     * @return Boolean
     */
    private boolean isExistFindByNameAndPwd(UserModel userModel) {
        UserModel userModel1 = userMapper.isExistFindByNameAndPwd(userModel);
        return userModel1 == null;
    }
}
